<?php

namespace App\Http\Controllers;

use App\Models\Replies;
use App\Models\Topics;
use App\Models\User;
use App\Notifications\Reply;
use App\Notifications\TopicReply;
use function gl\user_is_disabled;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RepliesController extends Controller
{

    /**
     * 发表评论
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function save(Request $request)
    {
        $user = Auth::user();
        if ($user->created_at->diffInSeconds() < config('custom.publish_time_limit')) {
            return response()->json(['status' => false, 'msg' => config('custom.not_publish_msg')]);
        }
        if(user_is_disabled($user)) {
            return response()->json(['status' => false, 'msg' => "您的账号被禁用{$user->disabled_duration}天，请{$user->disabled_duration}天以后再来发布信息"]);
        }
        $data          = [
            'topic_id' => $request->input('id'),
            'user_id'  => $user->id,
            'content'  => $request->input('reply_content'),
        ];
        $topic_id      = $request->input('id');
        $topic         = Topics::query()->find($topic_id);
        $res           = Replies::create($data);
        $replies_count = Replies::query()->where('topic_id', $topic_id)->count();
        $total_page    = ceil($replies_count / $request->input('page_size'));
        if ($res) {
            Topics::query()->where('id', $topic_id)->update(['reply_count' => $replies_count]);
            //通知
            $notied_user = User::find($topic->user_id);
            $notied_user->notify(new TopicReply($res));
            $reply_users = User::query()->whereIn('id',$request->input('reply_users'))->get();
            if ($reply_users->count() > 0) {
                foreach($reply_users as $replied_user) {
                    $replied_user->notify(new Reply($res));
                }
            }

            return response()->json(['status' => true, 'total_page' => $total_page, 'reply_count' => $replies_count]);
        }
    }

    public function get(Request $request)
    {
        $params     = $request->all();
        $offset     = ($params['curr_page'] - 1) * $params['page_size'];
        $total_page = ceil(Replies::query()->where('topic_id', $params['topic_id'])->count() / $params['page_size']);
        $replies    = Replies::query()
            ->where('topic_id', $params['topic_id'])
            ->with('user')
            ->offset($offset)
            ->limit($params['page_size'])
            ->get()->map(function ($item, $key) {
                $item->created = $item->created_at->diffForHumans();
                if (!@fopen($item->user->avatar, 'r')) {
                    $item->user->avatar = '/img/avatar.png';
                }
                if (Auth::check()) {
                    $item->can_delete = Auth::user()->can('delete', $item);
                } else {
                    $item->can_delete = false;
                }
                return $item;
            })->toArray();
        return response()->json(['status' => true, 'replies' => $replies, 'total_page' => $total_page]);
    }

    public function delete(Request $request)
    {
        $id  = $request->input('id');
        $reply = Replies::find($id);
        $res = Replies::destroy($id);
        if ($res) {
            $topic = Topics::find($reply->topic_id);
            $topic->reply_count = $topic->reply_count - 1;
            $topic->save();
            return response()->json(['status' => true,'reply_count' => $topic->reply_count]);
        } else {
            return response()->json(['status' => false]);
        }
    }
}
